#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
import os
import time
import random
from multiprocessing import Process, JoinableQueue, Lock


def production(q):
    time.sleep(0.2)
    for i in range(3):
        time.sleep(random.randint(1, 3))
        print("\033[45m生产者: {} 生产了: {} 号包子\033[0m".format(os.getpid(), i))
        q.put("{}.{}".format(os.getpid(), i))


def consumr(q):
    while True:
        time.sleep(random.randint(1, 3))
        res = q.get()
        # if res is None: break
        print("消费者: {} 消费了 {} 包子".format(os.getpid(), res))
        q.task_done()


if __name__ == '__main__':
    q = JoinableQueue()
    Lock()
    p1 = Process(target=production, args=(q,))
    p2 = Process(target=production, args=(q,))
    c1 = Process(target=consumr, args=(q,))
    c2 = Process(target=consumr, args=(q,))
    c1.daemon = True
    c2.daemon = True

    p1.start()
    p2.start()
    c1.start()
    c2.start()

    p1.join()
    p2.join()
    # c1.join()
    # c2.join()

    q.join()

    print("------主------")
